package com.liuheng.manager.controller;

import cn.hutool.core.date.DateUtil;
import com.liuheng.manager.properties.MinioProperties;
import com.liuheng.spzx.model.vo.common.Result;
import com.liuheng.spzx.model.vo.common.ResultCodeEnum;
import io.minio.BucketExistsArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.errors.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.UUID;

@RestController
@RequestMapping("/admin/system")
public class FileUploadController {
    @Autowired
    private MinioProperties properties;
    @PostMapping("fileUpload")
    public Result<String> fileUpload(@RequestParam(value = "file")MultipartFile file) {
        String fileName=null;
        MinioClient minioClient = MinioClient.builder()
                .endpoint(properties.getEndpointUrl())
                .credentials(properties.getAccessKey(), properties.getSecretKey())
                .build();
        try {
            boolean found = minioClient.bucketExists(BucketExistsArgs.builder().bucket(properties.getBucketName()).build());
            if(!found){
                minioClient.makeBucket(MakeBucketArgs.builder().bucket(properties.getBucketName()).build());
            }else{
                System.out.println("Bucker"+properties.getBucketName()+"already exists.");
            }
            // 设置存储对象名称
            String dateDir = DateUtil.format(new Date(), "yyyyMMdd");
            String uuid = UUID.randomUUID().toString().replace("-", "");
            //20230801/443e1e772bef482c95be28704bec58a901.jpg
             fileName = dateDir+"/"+uuid+file.getOriginalFilename();
            System.out.println(fileName);
            PutObjectArgs putObjectArgs = PutObjectArgs.builder()
                    .bucket(properties.getBucketName())
                    .stream(file.getInputStream(), file.getSize(), -1)
                    .object(fileName)
                    .build();
            minioClient.putObject(putObjectArgs) ;
            fileName= properties.getEndpointUrl() + "/" + properties.getBucketName() + "/" + fileName ;
            return Result.build(fileName, ResultCodeEnum.SUCCESS);
        } catch (ErrorResponseException e) {
            throw new RuntimeException(e);
        } catch (InsufficientDataException e) {
            throw new RuntimeException(e);
        } catch (InternalException e) {
            throw new RuntimeException(e);
        } catch (InvalidKeyException e) {
            throw new RuntimeException(e);
        } catch (InvalidResponseException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        } catch (ServerException e) {
            throw new RuntimeException(e);
        } catch (XmlParserException e) {
            throw new RuntimeException(e);
        }
    }
}
